package com.ui.element;

import java.awt.Color;
import java.awt.Rectangle;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JTextField;

import com.ui.event.HexKeyCall;

public class HexTextField extends JTextField {

	private static final long serialVersionUID = 4673274275189053952L;

	public HexTextField() {
		super();
		setFont(EleConsts.FONT_DEF);
	}

	public HexTextField(String text) {
		super(text);
		setFont(EleConsts.FONT_DEF);
		this.setForeground(Color.black);
	}

	/* 默认显示值 */
	/**
	 * @Description 设置空值时的默认显示
	 * @param @param text 参数
	 * @return void 返回类型
	 * @throws
	 */
	public void setPlaceholder(String text) {
		addFocusListener(focuAdp);
		Rectangle bs = this.getBounds();
		mask.setText(text);
		mask.setForeground(Color.lightGray);
		mask.setBounds(2, 0, bs.width - 2, bs.height - 2);
		this.add(mask);
	}

	FocusListener focuAdp = new FocusListener() {
		public void focusGained(FocusEvent arg0) {
			mask.setVisible(false);
		}

		public void focusLost(FocusEvent arg0) {
			String text = getText();
			if (text.trim().length() == 0) {
				mask.setVisible(true);
			}
		}
	};

	public void setText(String text) {
		super.setText(text);
		if (mask != null) {
			mask.setVisible(false);
		}
	}

	HexLabel mask = new HexLabel();
	/* 默认显示值 */

	/* 键盘监听 */
	private HexKeyCall call;

	KeyAdapter adap = new KeyAdapter() {
		public void keyPressed(KeyEvent e) {
			call.callBack(e);
		}
	};

	public void keyPressedCall(HexKeyCall call) {
		this.call = call;
		addKeyListener(adap);
	}
	/* 键盘监听 */

}
